using System;
using System.Data;
using System.Windows.Forms;

namespace DataStubGenerator.Views
{
    public partial class MainView : UserControl, IMainView
    {
        public MainViewPresenter MainViewPresenter
        {
            get { return _mainViewPresenter; }
        }

        private MainViewPresenter _mainViewPresenter;

        public MainView()
        {
            _mainViewPresenter = new MainViewPresenter(this,new FileModel());
            InitializeComponent();
        }

        #region IMainView Members

        public event EventHandler RunSql;
        public event EventHandler SaveTable;
        public event EventHandler LoadTableFromFile;
        public event EventHandler GenerateCode;

        public DataTable Table
        {
            get { return bindingSource1.DataSource as DataTable; }
            set
            {
                dataGridView1.Columns.Clear();
                if(value==null)
                    return;

                bindingSource1.SuspendBinding();
                bindingSource1.DataSource = value;

                foreach (DataColumn tableColumn in value.Columns) {
                    DataGridViewTextBoxColumn tmp = new DataGridViewTextBoxColumn();
                    tmp.DataPropertyName = tableColumn.ColumnName;
                    tmp.HeaderText = tableColumn.Caption;
                    dataGridView1.Columns.Add(tmp);
                }

                bindingSource1.ResumeBinding();
            }
        }

        public string SqlText
        {
            get { return sqlEditView1.Text; }
        }

        public string GetOpenFileName()
        {
            DialogResult res = openFileDialog1.ShowDialog(this);
            if (res == DialogResult.OK) 
                return openFileDialog1.FileName;
            else 
                return null;
        }

        public string GetSaveFileName()
        {
            DialogResult res = saveFileDialog1.ShowDialog(this);
            if (res == DialogResult.OK)
                return saveFileDialog1.FileName;
            else
                return null;
        }

        public void DisplayError(string msg, string caption)
        {
            MessageBox.Show(msg, caption);
        }


        private void RunSqlButton_Click(object sender, EventArgs e)
        {
            if (RunSql != null)
                RunSql(sender, e);
        }



        private void SaveButton_Click(object sender, EventArgs e)
        {
            if (SaveTable != null)
                SaveTable(sender, e);
        }

        private void LoadFromFileButton_Click(object sender, EventArgs e)
        {
            if (LoadTableFromFile != null)
                LoadTableFromFile(sender, e);
        }

        private void GenerateCodeButton_Click(object sender, EventArgs e)
        {
            if (GenerateCode != null)
                GenerateCode(sender, e);
        }
        #endregion


    }
}
